//
//  DYMSDingzuoView.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/5.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class DYMSDingzuoView: SYBaseView, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var current: (Int, String)? {
        didSet {
            clickedClosure?(current?.0 ?? 0)
        }
    }
    
    var collectionView: UICollectionView!
    var titles: [String] = []
    var isYudings: [Bool] = []

    public func configureView(_ titles: [String], _ isYudings: [Bool]? = nil) {
        self.titles = titles
        self.isYudings = isYudings ?? []
        self.collectionView.reloadData()
    }
    
    override func initViews() {
        
        setCollectionView()
        addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.left.equalToSuperview()
        }
        
    }
    
    
    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let itemWidth = 75.0.autoWidth()
        layout.itemSize = CGSize(width: itemWidth, height: 45.0.autoWidth())
        layout.minimumLineSpacing = 5
//        layout.minimumInteritemSpacing = 5
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(DYMSDingzuoSubCell.self, forCellWithReuseIdentifier: NSStringFromClass(DYMSDingzuoSubCell.self))
        self.addSubview(self.collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return titles.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: DYMSDingzuoSubCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(DYMSDingzuoSubCell.self), for: indexPath) as? DYMSDingzuoSubCell
        
        if let c = current, indexPath.row == c.0 {
            cell?.isChoosed = true
        } else {
            cell?.isChoosed = false
        }
        if isYudings.count != 0 {
            
            if !isYudings[indexPath.row] {
                cell?.isYuding = true
            }
        }
        
        cell?.configureView(self.titles[indexPath.row])
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if isYudings.count != 0 {
            if isYudings[indexPath.row] {
                current = (indexPath.row, titles[indexPath.row])
                self.collectionView.reloadData()
            } else {
                self.currentVC.sy_toast("您已经预订了此桌/该日期")
            }
        } else {
            current = (indexPath.row, titles[indexPath.row])
            self.collectionView.reloadData()
        }
    }
}




// MARK: -DYYuyueTimeSubCell

class DYMSDingzuoSubCell: SYBaseCollectionViewCell {
    
    var isChoosed: Bool = false {
        didSet {
            if isChoosed {
                timeLb.textColor = UIColor(hex: 0x5D99FB)
                timeLb.backgroundColor = UIColor(hex: 0xECF2FD)
                
            } else {
                timeLb.textColor = UIColor(hex: 0x333333)
                timeLb.backgroundColor = .white
            }
        }
    }
    var isYuding: Bool = false {
        didSet {
            if isYuding {
                timeLb.textColor = UIColor(hex: 0xF55035)
                timeLb.backgroundColor = Constant.bgViewColor
                
            } else {
                timeLb.textColor = UIColor(hex: 0x333333)
                timeLb.backgroundColor = .white
            }
        }
    }
    let timeLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 16.autoFontSize()
        v.textAlignment = .center
        v.layer.masksToBounds = true
        v.isUserInteractionEnabled = true
        v.layer.cornerRadius = 22.5.autoWidth()
        return v
    }()
    
    

    public func configureView(_ title: String) {
        
        timeLb.text = title
    }
    
    override func initViews() {

        addSubview(timeLb)
        
        timeLb.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            
        }
    }
}

